<?php
namespace app\warehouse\controller;
use think\Controller;
use think\Db; 
class Base extends Controller{
	public function _initialize(){		
		// 判断是否登录，没有登录跳转登录页面
		if(!session('user_auth') || !session('user_auth_sign')){
			$this->redirect('index/login');
		}
		$dispatch = $this->request->dispatch();
		$activeRouter = $dispatch['module']['0'] . '/' . $dispatch['module'][1] . '/' . $dispatch['module'][2];
		//echo $activeRouter;
		//exit();
		//$auth = new \com\Auth();
		//if(!$auth->check($activeRouter, session('user_auth')['uid'])){
		//	return $this->error('没有权限','index/index');
		//}
	}
    public function api_category_list(){
        $list = Db::table('warehouse_category')
        ->where('parent_id=0')->order('sort asc')
        ->select();
        foreach ($list as $key => $value) {
            unset($d);
            $d['name'] = $value['name'];
            $d['id'] = $value['id'];
            $d['sort'] = $value['sort'];
            $d['parent_id'] = $value['parent_id'];
            $d['description'] = $value['description'];
            $arr[] = $d;
            $lista = Db::table('warehouse_category')
            ->where('parent_id='.$value['id'])->order('sort asc')
            ->select();
            unset($a2);
            foreach ($lista as $keya => $valuea) {
                unset($d);
                $d['name'] = "└─" . $valuea['name'];
                $d['id'] = $valuea['id'];
                $d['sort'] = $valuea['sort'];
                $d['description'] = $valuea['description'];
                $d['parent_id'] = $valuea['parent_id'];
                $arr[] = $d;
                $listb = Db::table('warehouse_category')
                ->where('parent_id='.$valuea['id'])->order('sort asc')
                ->select();
                unset($a3);
                foreach ($listb as $keyb => $valueb) {
                    unset($d);
                    $d['name'] = "└─└─" . $valueb['name'];
                    $d['id'] = $valueb['id'];
                    $d['sort'] = $valueb['sort'];
                    $d['description'] = $valueb['description'];
                    $d['parent_id'] = $valueb['parent_id'];
                    $arr[] = $d;
                }
            }
        }
        return $arr;
	}
	public function api_get_category_name($cid){
		$info = Db::table('warehouse_category')
		->where('id='.$cid)
		->find();
		return $info['name'];
	}
	//生成唯一的物料编号
	public function api_make_pn(){
		for($i=0;$i<10;$i++){
			$pn = rand(100000,999999);
			$item = Db::table('warehouse_item')->where('no='.$pn)->find();
			if(!$item){
				$i=10;
				return $pn;
			}
		}

	}

}